package com.voxel.sense.infomsg.utils.manager;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.view.MyChartMarker;

import java.util.List;

/**
 * Created by ChenYiYao on 2018/10/8.
 * des :
 */
public class BarCharManager {

    public static void initBarChart(BarChart barChart, List<IBarDataSet> dataSets) {
        if (barChart != null && barChart != null && dataSets.size() > 0) {
            //设置图表的样式
            initDataStyle(barChart);
            XAxis xAxis = barChart.getXAxis();
            xAxis.setLabelCount(dataSets.size(),false);

            BarData barData = new BarData(dataSets);// 可以添加多个set，即可化成group组
            barData.setDrawValues(true);//绘制每个点
            barChart.setData(barData);
            //不在图形中直接显示值
            barData.setValueFormatter(new IValueFormatter() {
                @Override
                public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                    return "";
                }
            });
            //将数据插入表格中
            barChart.setData(barData);
            //设置动画效果
            barChart.animateY(1500, Easing.EasingOption.Linear);
            barChart.invalidate();
        }
    }

    /**
     * 初始化图表的样式
     *
     * @param barChart
     */
    private static void initDataStyle(BarChart barChart) {
        barChart.setFitBars(true);// x轴开头结尾两边添加bar一半宽的留白
        barChart.setScaleEnabled(false);//是否可缩放

        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置轴线位置在底部
        xAxis.setDrawGridLines(false);//隐藏网格线

        YAxis axisLeft = barChart.getAxisLeft();
        YAxis axisRight = barChart.getAxisRight();
        //是否绘画网格线
        axisLeft.setDrawGridLines(false);
        axisRight.setDrawGridLines(false);
        //设置起点位置，不然起点会有点上移
        axisRight.setAxisMinimum(0);
        axisLeft.setAxisMinimum(0);

        //图的legend
        Legend legend = barChart.getLegend();
        legend.setWordWrapEnabled(true);//legend是否换行

        //点击显示的窗口
        barChart.setMarker(new MyChartMarker(barChart.getContext(), R.layout.marker_chart));

    }


}
